package com.z.kq.controller;

import com.github.pagehelper.PageInfo;
import com.z.common.aop.annotation.Log;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.kq.vo.UserScheVo;
import com.z.kq.entity.Sche;
import com.z.kq.service.IScheService;
import com.z.kq.service.IScheSignService;
import com.z.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
 * 排班管理Controller
 *
 * @author z
 * @date 2023-03-28
 */
@RestController
@RequestMapping("/kq/sche")
public class ScheController extends BaseController {

    private String prefix = "kq/sche";

    @Autowired
    private IScheService scheService;
    @Autowired
    private IScheSignService scheSignService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/kq/sche/main','kq:sche:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询排班管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/kq/sche/data','kq:sche:main')")
    @Log(title = "排班管理", describe = "查询排班管理列表", type = BusinessType.QUERY)
    public Result list(@ModelAttribute Sche sche, @ModelAttribute User user, PageVo pageVo) {
        PageInfo<UserScheVo> pageInfo = scheService.selectUserSchePage(user, sche, pageVo);
        return Result.table(pageInfo);
    }
/*
    @GetMapping("/main")
    @PreAuthorize("hasPermission('/kq/sche/main','kq:sche:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    *//**
     * 查询排班管理列表
     *//*
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/kq/sche/data','kq:sche:main')")
    @Log(title = "排班管理", describe = "查询排班管理列表", type = BusinessType.QUERY)
    public Result list(@ModelAttribute Sche sche, PageVo pageVo) {
        PageInfo<Sche> pageInfo = scheService.selectSchePage(sche, pageVo);
        return Result.table(pageInfo);
    }*/
    /**
     * 根据班制类型查询班制名称
     */
    @ResponseBody
    @GetMapping("/scheNames")
    @PreAuthorize("hasPermission('/kq/sche/scheNames','kq:sche:main')")
    public Result scheNames(String scheType, String unitId) {
        List<String> list = scheSignService.selectScheNames(scheType, unitId);
        return Result.success(list);
    }

    /**
     * 根据班制名称查询班段名称
     */
    @ResponseBody
    @GetMapping("/scheStages")
    @PreAuthorize("hasPermission('/kq/sche/scheNames','kq:sche:main')")
    public Result scheStages(String scheName, String scheType, String unitId) {
        List<String> list = scheSignService.selectScheStages(scheName, scheType, unitId);
        return Result.success(list);
    }

    /**
     * 新增排班管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/kq/sche/add','kq:sche:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增排班管理
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/kq/sche/save','kq:sche:add')")
    @Log(title = "排班管理", describe = "新增排班数据", type = BusinessType.ADD)
    public Result save(@RequestBody Sche sche) {
        return Result.decide(scheService.save(sche));
    }

    /**
     * 修改排班管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/kq/sche/edit','kq:sche:edit')")
    public ModelAndView edit(Long id, ModelMap map) {
        Sche sche =scheService.getById(id);
        map.put("sche", sche);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改排班管理
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/kq/sche/update','kq:sche:edit')")
    @Log(title = "排班管理", describe = "更新排班数据", type = BusinessType.EDIT)
    public Result update(@RequestBody Sche sche) {
        return Result.decide(scheService.updateById(sche));
    }

    /**
     * 删除排班管理
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/kq/sche/batchRemove','kq:sche:del')")
    @Log(title = "排班管理", describe = "批量删除排班数据", type = BusinessType.REMOVE)
    public Result batchRemove(String ids) {
        return Result.decide(scheService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除排班管理
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/kq/sche/remove','kq:sche:del')")
    @Log(title = "排班管理", describe = "删除数据", type = BusinessType.REMOVE)
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(scheService.removeById(id));
    }
}
